﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QuanLyNhaSach.BusinessLogicLayer;

namespace QuanLyKhachHang_NS
{
    public partial class QuanLyKhachHang : Form
    {
        public QuanLyKhachHang()
        {
            InitializeComponent();
            this.SuspendLayout();
            // 
            // QuanLyKhachHang
            // 
            //this.ClientSize = new System.Drawing.Size(460, 261);
            this.Name = "QuanLyKhachHang";
            this.ResumeLayout(false);

        }

        private void Them_Click(object sender, EventArgs e)
        {
            
            txt_tenkh.BackColor = Color.White;
            txt_diachi.BackColor = Color.White;
            txt_sdt.BackColor = Color.White;

            bool error = false;
            if (txt_tenkh.Text == "")
            {
                txt_tenkh.BackColor = Color.Red;
                error = true;
            }

            if (txt_diachi.Text == "")
            {
                txt_diachi.BackColor = Color.Red;
                error = true;
            }
            if (txt_sdt.Text == "")
            {
                txt_sdt.BackColor = Color.Red;
                error = true;
            }
            if (error) return;
            txt_tenkh.BackColor = Color.White;
            txt_diachi.BackColor = Color.White;
            txt_sdt.BackColor = Color.White;
            QuanLyKhachHangBLL a = new QuanLyKhachHangBLL();
            a.ThemKhachHang(txt_makh.Text, txt_tenkh.Text, txt_diachi.Text, txt_sdt.Text);
            txt_makh.Text = a.TaoMaKhachHang();
            txt_tenkh.Text = "";
            txt_diachi.Text = "";
            txt_sdt.Text = "";
            
          
        }

        private void QuanLyKhachHang_Load(object sender, EventArgs e)
        {
            QuanLyKhachHangBLL kh = new QuanLyKhachHangBLL();
            DataTable data = kh.LayDanhSach();
            list_danh_sach_kh.DataSource = data;
            list_danh_sach_kh.DisplayMember = "TenKH";
           
            txt_makh1.DataBindings.Add("text", data, "MaKH");
            txt_tenkh1.DataBindings.Add("text", data, "TenKH");
            txt_diachi1.DataBindings.Add("text", data, "DiaChi");
            txt_sdt1.DataBindings.Add("text",data, "SDT");

            tim_kiem.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            tim_kiem.AutoCompleteSource = AutoCompleteSource.CustomSource;
            tim_kiem.AutoCompleteCustomSource = kh.TaoAutocompleteTen();
            txt_makh1.Text = kh.TaoMaKhachHang();

    
            txt_makh.Text = kh.TaoMaKhachHang();
        }

        private void txt_makh_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            QuanLyKhachHangBLL kh = new QuanLyKhachHangBLL();
            kh.ThayDoiTTKH(txt_makh1.Text, txt_tenkh1.Text, txt_diachi1.Text, txt_sdt1.Text);

            MessageBox.Show("Cập nhật thông tin khách hàng thành công");
        }
        
     

        private void tim_kiem_TextChanged_1(object sender, EventArgs e)
        {
            QuanLyNhaSach.BusinessLogicLayer.QuanLyKhachHangBLL kh = new QuanLyNhaSach.BusinessLogicLayer.QuanLyKhachHangBLL();
            DataTable data = kh.TimKiemTen(tim_kiem.Text);
            list_danh_sach_kh.DataSource = data;
            list_danh_sach_kh.DataSource = data;
            list_danh_sach_kh.DisplayMember = "TenKH";

            txt_makh1.DataBindings.Clear();
            txt_tenkh1.DataBindings.Clear();
            txt_diachi1.DataBindings.Clear();
            txt_sdt1.DataBindings.Clear();

            txt_makh1.DataBindings.Add("text", data, "MaKH");
            txt_tenkh1.DataBindings.Add("text", data, "TenKH");
            txt_diachi1.DataBindings.Add("text", data, "DiaChi");
            txt_sdt1.DataBindings.Add("text", data, "SDT");

        }

        private void tim_kiem_Click_1(object sender, EventArgs e)
        {
            tim_kiem.Text = "";
        }

        
    }
}
